<template>
    <el-dialog :title="$t('game.config.betting.dialog.sync')" width="500px" :visible.sync="dialogVisible" @close="$emit('update:visible', false)">
        <div class="pr-3">
            <el-form :model="dataCache" label-width="auto">
                <div class="mb-6">
                    <el-alert type="warning" :title="$t('game.config.betting.sync.desc')" :closable="false"></el-alert>
                </div>
                <el-form-item :label="$t('game.config.betting.sync.source')" :error="Boolean(msg.errors['sourceGameId']) ? msg.errors['sourceGameId'][0] : ''" required>
                    <component-page-game-option v-model="dataCache.sourceGameId" class="block" :multiple="false"></component-page-game-option>
                </el-form-item>
                <el-form-item :label="$t('game.config.betting.sync.target')" :error="Boolean(msg.errors['targetGameIds']) ? msg.errors['targetGameIds'][0] : ''" required>
                    <component-page-game-option v-model="dataCache.targetGameIds" class="block"></component-page-game-option>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer">
            <el-button @click="dialogVisible = false">{{ $t('action.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ $t('action.sync') }}</el-button>
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: "ItemSync",
    props: ['visible'],
    data() {
        return {
            dialogVisible: false,
            dataCache: {
                sourceGameId: '',
                targetGameIds: [],
            },
            dataInit: {},
            msg: {
                code: 200,
                message: '',
                errors: {},
            },
        }
    },
    watch: {
        visible: function (n, o) {
            if (n) {
                // 初始化参数
                this.initData();
                // 显示模态框
                this.dialogVisible = n;
            }
        }
    },
    created() {
        this.dataInit = _.cloneDeep(this.dataCache);
    },
    methods: {
        initMsg() {
            this.msg.code = 200;
            this.msg.message = '';
            this.msg.errors = {};
        },
        initData() {
            this.initMsg();
            this.dataCache = _.cloneDeep(this.dataInit);
        },
        onSubmit() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/game/config/betting/fullGameBets', this.dataCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.sync')}),
                        showClose: true
                    });
                    this.dialogVisible = false;
                } else if (_.includes([42000], response.data.resp_msg.code)) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.sync')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>
::v-deep .el-dialog__body {
    padding-top: 15px;
    padding-bottom: 15px;
    min-height: 330px;
}
</style>
